/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($rootScope, $scope, customerBarService,$cookies, paginationService) {
  $rootScope.showCaseDiv = false;
  $rootScope.closeModal();

  $scope.validateOptions = {
    blurTrig: true,
    showError: false,
    removeError: true
  };
    /*登录ID*/
    $scope.loginId = kbt.decrypt($cookies.get('cookieUserId'));

    /*当前页*/
    $scope.page = 1;

    /*每页显示的条数*/
    $scope.pageSize = '10';

    //搜索框
    $scope.seaName = '';

    /*获取搜索的内容*/
    $scope.workplace = '';

    /*工作单位*/
    $scope.zrrsex = '';
    $scope.frsex = '';


    //自然人法人切换显示
    $scope.tabShow = 0;
    $scope.getCustomerLeft = getCustomerLeft;
    $scope.getCustomerLeft2 = getCustomerLeft2;
    $scope.getPersonListGroup = getPersonListGroup;
    $scope.customerTabShowT=customerTabShowT;
    //添加客户弹框
    $scope.personActive = 0;
    $scope.showTeam = showTeam;
    $scope.pageSetting = paginationService.getDefaultPageSetting();
    $scope.pageSetting2 = paginationService.getDefaultPageSetting();
    $scope.findEnterpriseList = findEnterpriseList;

  //自然人编辑 (获取当前自然人详细信息)接口
  $scope.editPerson = editPerson;
  //自然人编辑关闭
  // $scope.editPersonClose = editPersonClose;
  //自然人编辑身份信息
  $scope.naturalPerEditInfor = naturalPerEditInfor;
  //自然人编辑身份信息取消
  $scope.naturalPerEditInforCancel = naturalPerEditInforCancel;
  //机构编辑身份信息取消
  $scope.organizaEditInforCancel = organizaEditInforCancel;
  //机构信息编辑
  $scope.organizaEditInfor = organizaEditInfor;
  //保存自然人编辑身份信息 接口
  $scope.saveEditPersonIdInfor = saveEditPersonIdInfor;
  //保存机构编辑身份信息 接口
  $scope.saveEditOrganizaInfor = saveEditOrganizaInfor;
  //删除自然人 接口
  $scope.deleteZRRCustomer = deleteZRRCustomer;
  //删除机构 接口
  $scope.deleteFRCustomer = deleteFRCustomer;
  //机构编辑 (获取当前机构详细信息)接口
  $scope.editOrganiza = editOrganiza;
  //创建案件
  $scope.createCase = createCase;
  //编辑分组
  $scope.editGroupPort = editGroupPort;
  $scope.editGroupPortSec = editGroupPortSec;
  //保存编辑分组 接口
  $scope.saveEditGroup = saveEditGroup;
  //删除分组 接口
  $scope.deleteGroup = deleteGroup;
  //显示性别
  $scope.gender = gender;
  //数据清理
  $scope.createEmpty=createEmpty;


    //显示性别
    function gender(sex) {
      if(sex == 0){
        return '男';
      }else if(sex == 1){
        return '女';
      }
    }
  sessionStorage.removeItem('customerGroupId');
  sessionStorage.removeItem('customerGroupId2');
    //客户左侧列表显示
    getPersonListGroup();
    function getPersonListGroup() {
      customerBarService.getPersonListGroup().then(function (response) {
        if(response.data.code == 1){
          $scope.customerList = response.data.data;
          $scope.listActive = sessionStorage.getItem('customerGroupId')||$scope.customerList[0].customerGroupId;
          $scope.customerListNum = response.data.totalCount;
          console.log($scope.listActive,'再次渲染')
          /*查询自然人的基本信息接口*/
          $scope.selectZiRanRenInfo();
          $scope.groupName = response.data.data[0].customerGroupName;
          $scope.teamSelected = response.data.data[0].customerGroupId;
        }
      });
    }
    //客户自然人左侧列表点击事件
    function getCustomerLeft(customerGroupName,index) {
      if(index){
        $scope.listActive = index;
        $scope.paramsSearchP = null;
        sessionStorage.setItem('customerGroupId',index);
      }
      $scope.groupName = customerGroupName;
      $scope.pageSetting = paginationService.getDefaultPageSetting();
      $scope.selectZiRanRenInfo();

    }
    //客户机构左侧列表点击事件
    function getCustomerLeft2(customerGroupName,index) {
      if(index){
        $scope.listActive2 = index;
        $scope.paramsSearchC = null;
        sessionStorage.setItem('customerGroupId2',index);
      }
      $scope.groupName2 = customerGroupName;
      $scope.pageSetting2 = paginationService.getDefaultPageSetting();
      $scope.selectFaRenInfo();
    }
    function customerTabShowT(){
      $scope.tabShow=0;
      $scope.personActive=0;
      // sessionStorage.removeItem('customerGroupId2');
      $scope.selectZiRanRenInfo();
    }

    function showTeam(index) {
      $scope.teamSelected = index;
    }

    /*新增用户*/
    //新增自然人机构用户
    $scope.paramsP = {};
    $scope.paramsM = {};
    $scope.createPersonList = createPersonList;
    $scope.createCompanyList = createCompanyList;
    $scope.addCustomerGroup = addCustomerGroup;
    $scope.addParams = {};
    function createEmpty(){
      $scope.paramsP={
        'name':'',
        'contactnumber':'',
        'identificationNumber':'',
        'profession':'',
        'workplace':'',
        'address':'',
        'age':'',
        'nationality':''
      };
      $scope.paramsM={
        contactphone:''
      }
      $scope.companyForm.reset();
        /*$scope.paramsP.name='';
        $scope.paramsP.contactnumber='';
        $scope.paramsP.identificationNumber='';
        $scope.paramsP.profession='';
        $scope.paramsP.workplace='';
        $scope.paramsP.address='';
        $scope.paramsP.age='';
        $scope.paramsP.nationality='';*/

    }
    function createPersonList() {
      $scope.paramsP.customerGroupId = $scope.teamSelected;
      customerBarService.addZRRCustomer($scope.paramsP).then(function (data) {
        if(data.code == 1){
          $('#myModalCust').modal('hide');
          $scope.getPersonListGroup();
          $scope.paramsP = {};
        }else{
          $scope.paramsP = {};
        }
      });
    }
    function createCompanyList() {
      $scope.paramsM.customerGroupId = $scope.teamSelected;
      customerBarService.addFRCustomer($scope.paramsM).then(function (data) {
        if(data.code == 1){
          $('#myModalCust').modal('hide');
          $scope.selectFaRenInfoList();
          $scope.paramsM = {};
        }else{
          // $scope.paramsM = {};
        }
      });
    }

    //新增左侧分组
    function addCustomerGroup() {
      if($scope.addParams.groupId!=""){
        var params = {'groupId':$scope.addParams.groupId,'groupName':$scope.addParams.groupName};
        customerBarService.addCustomerGroup(params).then(function (response) {
          if(response.data.code==1){
            if($scope.editTeamType==0){
              getPersonListGroup();
            }else{
              getPersonListGroup();
              $scope.selectFaRenInfoList();
            }
            $scope.createTeam = false;
          }
        })

      }else{
        customerBarService.addCustomerGroup($scope.addParams).then(function (response) {
          if(response.data.code == 1){
            $scope.addParams = {};
            if($scope.editTeamType==0){
              getPersonListGroup();
            }else{
              getPersonListGroup();
              $scope.selectFaRenInfoList();
            }
            $scope.createTeam = false;
          }
        });
      }

    }

  function createCase(event,item, type) {
    $scope.selectConsigner(item, type);
    event.stopPropagation();
    $scope.newXin();
  }

  //添加客户机构的下拉
  function findEnterpriseList() {
    var params = {
      entName:$scope.paramsM.companyname
    };
    return customerBarService.findEnterpriseList(params).then(function (response) {
      if(response.data.returnCode == 200){
       return  response.data.data;
      }
    });
  }

    //界面,
    // 自然人和法人
    $scope.zusers = '';
    $scope.fusers = '';
    //详情
    //自然人
    $scope.id = '';
    $scope.zdetail = '';
    $scope.zdname = '';
    $scope.zdsex = '';
    $scope.zdphone = '';
    $scope.zdbirthday = '';
    $scope.zdnationality = '';
    $scope.zdprofession = '';
    $scope.zdworkplace = '';
    $scope.zdadd = '';
    $scope.zfremark = '';
    $scope.zmodifieddate = '';

    //法人
    $scope.id = '';
    $scope.fdetail = '';
    $scope.fdname = '';
    $scope.fdadd = '';
    $scope.fddb = '';
    $scope.fdzw = '';
    $scope.fdlxr = '';
    $scope.fdbz = '';
    $scope.fdphone = '';
    $scope.fmodifieddate = '';


    //添加用户
    //自然人
    $scope.zname = '';
    $scope.zsex = '';
    $scope.zphone = '';
    $scope.zbirthday = '';
    $scope.znationality = '';
    $scope.zprofession = '';
    $scope.zworkplace = '';
    $scope.zaddress = '';
    $scope.zstatus = '1';
    $scope.zremark = '';
    //法人
    $scope.fname = '';
    $scope.fadd = '';
    $scope.frep = '';
    $scope.fpost = '';
    $scope.fcname = '';
    $scope.fcphone = '';
    $scope.fworkplace = '';
    $scope.fstatus = '1';
    $scope.fremark = '';

    //判断当前界面
    var currentPage = 1;

    $scope.init = function () {

        /*=====关闭右侧弹出框====*/
        $(".delateicon").click(function () {
            $(this).parents(".slidright-body").animate({
                "right": "-470"
            }, 300);
        });

        //左右切换自然人和法人
        $('.title-nav ul li').on('click', function (e) {

            /**切换自然人法人时关闭弹框*/
            $(".delateicon").parents(".slidright-body").animate({
                "right": "-470"
            }, 300);

            var target = $(this);
            var which = parseInt(target.val());
            target.addClass("active");
            target.siblings().removeClass("active");
            $('[id*="tab-"]').addClass('hide');
            $('#tab-' + which).removeClass('hide');
            if ($('.title-nav ul li').eq(0).hasClass('active')) {
                /*查询自然人的基本信息接口*/
                $scope.selectZiRanRenInfo();

            } else {
                /*查询法人的基本信息接口*/
                $scope.selectFaRenInfo();

            }
        });


    };

    /*查询自然人的基本信息接口*/
    $scope.selectZiRanRenInfo = function () {
        var params = {'pageNum': $scope.pageSetting.pageNo, 'pageSize': $scope.pageSetting.pageSize,'name':$scope.seaName,'customerGroupId':$scope.listActive,'customerName':$scope.paramsSearchP};
        if(!$scope.paramsSearchP){
          customerBarService.selectZiRanRenInfo(params).then(function (response) {
            if (response.data.code == 1) {
              if(response.data.totalCount){
                $scope.showBlank = false;
                $scope.zrRestult = response.data.data;
                $scope.pageSetting.totalItems = response.data.totalCount
              }else{
                $scope.showBlank = true;
              };
            }else{
              if(!response.data.totalCount){
                $scope.showBlank = true;
              }
            }

            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
          });
        }else{
            customerBarService.searchPersonCustomer(params).then(function (response) {
              $scope.showBlank = false;
              if(response.data.code == 1){
                $scope.listActive = null;
                $scope.showBlankSearch = false;
                if(response.data.totalCount){
                  // $scope.showBlankSearch = false;
                  $scope.zrRestult = response.data.data;
                };
              }else{
                if(!response.data.totalCount){
                  $scope.listActive = null;
                  // $scope.showBlankSearch = true;
                }
              }
              $scope.pageSetting.totalItems = response.data.totalCount;
            });
          // }
        }
        $rootScope.loadingShow = false;
        $rootScope.fadeShow = false;

    };

    /*点击右上角查询接口*/
    /*$scope.searchTextClick = function () {

        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        if (currentPage == 1){
            //模糊查询自然人
            customerBarService.selectZiRanRenInfo().then(function (data) {
                $scope.seaName = '';
                // if (data.code == 1) {
                //     $scope.zusers = data.data.list;
                //     $scope.totalPage = data.data.pages;//总页数
                //     $scope.totalPas = data.data.navigatepageNums;//遍历的数组
                //     $scope.totalCount = data.data.total;//总条数
                //     setTimeout(function () {
                //         $('.select' + $scope.page).css('color', 'red');
                //     }, 50);
                //     changeSex(data.data);
                // }
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
            }, function () {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
            });
        }else {
            //模糊查询法人
            customerBarService.selectFaRenInfo($scope.loginId, $scope.pageSize, $scope.page,$scope.seaName).then(function (data) {
                $scope.seaName = '';
                // if (data.code == 1) {
                //     $scope.fusers = data.data.list;
                //     $scope.totalPage = data.data.pages;//总页数
                //     $scope.totalPas = data.data.navigatepageNums;//遍历的数组
                //     $scope.totalCount = data.data.total;//总条数
                //     currentPage = 2;
                //     changeSex(data.data);
                // }
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
            }, function () {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
            });
        }
    };*/


    // function changeSex(data) {
    //     var sexes = data;
    //
    //     setTimeout(function(){
    //         for(var i=0;i<sexes.length;i++){
    //             if(sexes[i].sex===0){
    //                 $("."+sexes[i].id).attr('src','img/menicon.png');
    //             }else{
    //                 $("."+sexes[i].id).attr('src','img/womenicon.png');
    //             }
    //         }
    //     },50);
    // }
    /*查询机构的基本信息接口*/
    $scope.selectFaRenInfoList = function () {
      sessionStorage.removeItem('customerGroupId')
      getCompanyListGroup();
      function getCompanyListGroup() {
        customerBarService.getGroupCompanyList().then(function (response) {
          if(response.data.code == 1){
            $scope.customerList2 = response.data.data;
            $scope.listActive2 = sessionStorage.getItem('customerGroupId2')||$scope.customerList[0].customerGroupId;
            $scope.customerListNum2 = response.data.totalCount;
            /*查询机构的基本信息接口*/
            $scope.selectFaRenInfo();
          }

        });
      }
    };
    $scope.selectFaRenInfo = function () {
        $rootScope.loadingShow = true;
        $rootScope.fadeShow = true;
        $scope.tabShow=1;
        var params = {'pageNum': $scope.pageSetting2.pageNo, 'pageSize': $scope.pageSetting2.pageSize, 'biglawuserid': $scope.loginId, 'name':$scope.seaName,'customerGroupId':$scope.listActive2,'customerName':$scope.paramsSearchC};
        if(!$scope.paramsSearchC){
          customerBarService.selectFaRenInfo(params).then(function (response) {
            if (response.data.code == 1) {
              if(response.data.totalCount){
                $scope.showBlank = false;
                $scope.jgResult = response.data.data;
                $scope.pageSetting2.totalItems = response.data.totalCount
              };
              $scope.pageSetting2.totalItems = response.data.totalCount;
            }else{
              if(!response.data.totalCount){
                $scope.showBlank = true;
              }
            }
            $rootScope.loadingShow = false;
            $rootScope.fadeShow = false;
          });
        }else{
          customerBarService.searchCustomerCustomer(params).then(function (response) {
            $scope.showBlank = false;
            if(response.data.code == 1){
              $scope.listActive2 = null;
              $scope.showBlankSearch = false;
              if(response.data.totalCount){
                // $scope.showBlankSearch = false;
                $scope.jgResult = response.data.data;
              };
            }else{
              if(!response.data.totalCount){
                $scope.listActive2 = null;
                // $scope.showBlankSearch = true;
              }
            }
            $scope.pageSetting2.totalItems = response.data.totalCount;
          });
        }
        $rootScope.loadingShow = false;
        $rootScope.fadeShow = false;

    };
    /*分类应用列表点击改变页码*/
    $scope.changePage = function (currentPage) {
        if(currentPage==undefined){
            currentPage = 1;
        }
        if (currentPage <= $scope.totalPage && currentPage > 0) {
            /*判断当前显示的是法人还是自然人*/
            if ($('.title-nav ul li').eq(0).hasClass('active')) {
                /*查询自然人的基本信息接口*/
                $scope.selectZiRanRenInfo();
            } else {
                /*查询法人的基本信息接口*/
                $scope.selectFaRenInfo($scope.loginId, $scope.pageSize, currentPage);
            }
            $scope.page = currentPage;
        }

        else {
            console.log('列表页码越界!');
        }

    }
    //默认选择男
    $scope.naturename1=true;

    /*自然人客户弹出*/
    /*$scope.showDetailBox1 = function (id) {

        $("#edit-custMessage").animate({
            "right": "0"
        }, 400);

        //展示详情
        //请求详情接口
        customerBarService.ZRRDetail(id).then(function (data) {
            if (data.code == 1) {
                //成功
                $scope.zdetail = data.data;
                $scope.zdname = $scope.zdetail.name;
                if ($scope.zdetail.sex == 0) {
                    $scope.zdsex = '男';
                    $scope.naturename1=true;
                    $scope.naturename2=false;
                } else {
                    $scope.zdsex = '女';
                    $scope.naturename1=false;
                    $scope.naturename2=true;
                }
                $scope.id = $scope.zdetail.id;
                $scope.zdphone = $scope.zdetail.contactnumber;
                $scope.zdbirthday = $scope.zdetail.createddate;
                $scope.zdnationality = $scope.zdetail.nationality;
                $scope.zdprofession = $scope.zdetail.profession;
                $scope.zdworkplace = $scope.zdetail.workplace;
                $scope.zdadd = $scope.zdetail.address;
                $scope.zdremark = $scope.zdetail.remark;
            }
        });
        $(".cust-hide").show();
        $(".message").hide();
        $("#editBtn span").html('完成');
    };*/

    /*======法人客户弹出=========*/
    /*$scope.showDetailBox2 = function (id) {
        $("#edit-custMessage2").animate({
            "right": "0"
        }, 400);

        //请求详情接口
        customerBarService.FRDetail(id).then(function (data) {
            if (data.code == '1') {
                //成功
                $scope.fdetail = data.data;
                $scope.id = $scope.fdetail.id;
                $scope.fdname = $scope.fdetail.companyname;
                $scope.fdadd = $scope.fdetail.address;
                $scope.fddb = $scope.fdetail.representative;
                $scope.fdzw = $scope.fdetail.post;
                $scope.fdlxr = $scope.fdetail.contactname;
                $scope.fdbz = $scope.fdetail.remark;
                $scope.fdphone = $scope.fdetail.contactphone;
            }
        });
        $(".cust-hide").show();
        $(".message").hide();
        $("#editBtn2 span").html('完成');
    };*/


    /*新增用户的类型*/
    $("#add-newcust").click(function () {
        /*判断当前显示的是法人还是自然人*/
        if ($('.cust-typeText').html() == '自然人客户') {
            /*自然人*/
            $('.ziranXinXi').show();
            $('.farenShenFen').hide();

        } else {
            /*法人*/
            $('.farenShenFen').show();
            $('.ziranXinXi').hide();
        }
        $("#add-newBox").animate({
            "right": "0"
        }, 300);
    });
    /*======选择客户类型==========*/
    $(".cust-typeList li").click(function () {
        var text = $(this).text();
        if (text == '自然人客户') {
            $('.ziranXinXi').show();
            $('.farenShenFen').hide();
        } else {
            $('.farenShenFen').show();
            $('.ziranXinXi').hide();
        }
        $(this).parents(".coust-tpe-dropdown").find(".cust-typeText").html(text);

    });


    /*========自然人编辑======*/
    $("#editBtn").click(function () {

        // console.log('编辑');

        if ($("#editBtn span").html() == '编辑') {
            $(".cust-hide").show();
            $(".message").hide();
            $("#editBtn span").html('完成');
        } else {
            $(".cust-hide").hide();
            $(".message").show();
            $("#editBtn span").html('编辑');


            if ($("#zdman").is(":checked")) {
                $scope.zrrsex = '0';
            }
            //如果是女
            if ($("#zdgirl").is(":checked")) {
                $scope.zrrsex = '1';
            }

            $('.errorHint').css('display','none');
            //如果手机号码正确
            if(checkPhone($scope.zdphone) || $scope.zdphone == ''){
               /* //修改时间
                var dt = new Date($scope.zmodifieddate.replace(/-/,"/"));
                //出生日期
                var db = new Date(  $scope.zdbirthday.replace(/-/,"/"));*/
               //出生年月
               // $scope.dt = new Date($scope.zdbirthday).pattern("yyyy-MM-dd hh:mm:ss");

                //编辑之后发送请求
                customerBarService.ZRREditorUrl($scope.id, $scope.zdname, $scope.zrrsex, $scope.zdphone,$scope.zdbirthday,
                    $scope.zdnationality, $scope.zdprofession, $scope.zdworkplace, $scope.zdadd,
                    '1', $scope.zfremark).then(function (data) {
                    if (data.code == 1) {
                        /*刷新查询自然人的基本信息接口*/
                        $scope.selectZiRanRenInfo();
                        $("#edit-custMessage").animate({
                            "right": "-470"
                        }, 300);
                    }

                });
            }else {
                $('.errorHint').css('display','block');
            }

        }
    });
    /*========法人编辑======*/
    $("#editBtn2").click(function () {

        $scope.fmodifieddate = new Date().pattern("yyyy-MM-dd hh:mm:ss");

        if ($("#editBtn2 span").html() == '编辑') {
            $(".cust-hide").show();
            $(".message").hide();
            $("#editBtn2 span").html('完成');
        } else {
            $(".cust-hide").hide();
            $(".message").show();
            $("#editBtn2 span").html('编辑');

            $('.errorHint').css('display','none');
            if(checkPhone($scope.fdphone) || $scope.fdphone == ''){
                //编辑之后发送请求
                customerBarService.FREditorUrl($scope.id, $scope.fdname, $scope.fdadd, $scope.fddb, $scope.fdzw, $scope.fdlxr,
                    $scope.fdbz, $scope.fdphone, $scope.fmodifieddate).then(function (data) {
                    if (data.code == 1) {
                        /*刷新查询自然人的基本信息接口*/
                        $scope.selectFaRenInfo($scope.loginId, $scope.pageSize, '1');
                        $("#edit-custMessage2").animate({
                            "right": "-470"
                        }, 300);
                    }
                });
            }else {
                $('.errorHint').css('display','block');
            }

        }


    });
    /*========法人关闭弹出框======*/
    $('.delateicon_2').click(function () {
        if ($("#editBtn2 span").html() == '完成') {
            $(".cust-hide").hide();
            $(".message").show();
            $("#editBtn2 span").html('编辑');
        }
    });
    /*========自然人关闭弹出框======*/
    $('.delateicon_1').click(function () {
        if ($("#editBtn span").html() == '完成') {
            $(".cust-hide").hide();
            $(".message").show();
            $("#editBtn span").html('编辑');
        }
    });
    /*自然人删除*/
    $('.delate-custicon_1').click(function () {
      //发送请求
      customerBarService.deleteZRRCustomer($scope.id).then(function (data) {

        $("#edit-custMessage").animate({
          "right": "-470"
        }, 300);

        if (data.code == 1) {
          //刷新下自然人界面
          /*查询自然人的基本信息接口*/
          $scope.selectZiRanRenInfo();
        } else {
          alert("删除自然人失败");
        }
      });

    });
    /*法人删除*/
    $('.delate-custicon_2').click(function () {
        //发送请求
        customerBarService.deleteFRCustomer($scope.id).then(function (data) {
            $("#edit-custMessage2").animate({
                "right": "-470"
            }, 300);

            if (data.code == 1) {
                //刷新下法人界面
                /*查询法人的基本信息接口*/
                $scope.selectFaRenInfo($scope.loginId, $scope.pageSize, '1');

            } else {
                alert("删除法人失败");
            }
        });
    });

    /*$scope.addListPeoPle = function () {
        // $('.errorHint').css('display','none');
        if (personActive==0) {
            //添加自然人客户
            //如果是男
           /!* if ($("#zman").is(":checked")) {
                $scope.zsex = 0;
            }
            //如果是女
            if ($("#zgirl").is(":checked")) {
                $scope.zsex = '1';
            }*!/
            //如果手机号码正确
            if(checkPhone($scope.zphone) || $scope.zphone == ''){
                //发送请求
                customerBarService.addZRRCustomer(paramsP).then(function (data) {
                    $("#add-newBox").animate({
                        "right": "-470"
                    }, 300);
                    if (data.code == 1) {
                        //刷新下自然人界面
                        /!*查询自然人的基本信息接口*!/
                        $scope.selectZiRanRenInfo();
                    } else {
                        alert("新增自然人失败");
                    }
                });
            }else {
                $('.errorHint').css('display','block');
            }

        }else{

            //如果手机号码正确
            if(checkPhone($scope.zphone) || $scope.zphone == ''){
                //添加法人客户
                customerBarService.addFRCustomer($scope.loginId, $scope.fname, $scope.fadd, $scope.frep, $scope.fpost, $scope.fcname,
                    $scope.fcphone, $scope.fworkplace, $scope.fstatus, $scope.fremark).then(function (data) {
                    $("#add-newBox").animate({
                        "right": "-470"
                    }, 300);
                    //刷新下法人界面
                    /!*查询法人的基本信息接口*!/
                    $scope.selectFaRenInfo($scope.loginId, $scope.pageSize, '1');
                });
            }else {
                $('.errorHint').css('display','block');
            }
        }

    };*/

    /**
     * 日期选择器
     */
    $(".form_datetime").datetimepicker({
        minView: "month",
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        charset:"utf-8"
    });

    /**
     * 更改日期事件
     */
    $('.form_datetime')
        .datetimepicker()
        .on('changeDate', function(ev){
            $('.form_datetime').datetimepicker('hide');
        });

    $scope.init();

    //自然人编辑 接口

  function editPerson(customerpersonId) {

    $('.z-Basic').css('display','block');
    $('.z-Basic-left').css('display','block');
    $('.z-Basic-header').css('display','block');
    $('.individual').css('display','block');
    $('.enterprise').css('display','none');
    $('.z-Basic-content').css('display','block');
    $('.details').css('display','block');
    $('.natural-on').css('display','block');
    $('.mechanism-on').css('display','none');
    $('.z-Basic-right').css('display','block');
    $('.sidebar-natural').css('display','block');
    $('.sidebar-mechanism').css('display','none');
    var params = {'id':customerpersonId};
    customerBarService.getPersonDetail(params).then(function (response) {
      if(response.data.code == 1){
        $scope.editNaturalPersonInfo = response.data.data.detail;
        $scope.levelsPer = response.data.data.levels;
        $scope.tradesPer = response.data.data.trades;
        $scope.customerpersonId = $scope.editNaturalPersonInfo.id;
        $scope.customerGroupId = $scope.editNaturalPersonInfo.customerGroupId;
        $scope.customerGroupId = response.data.data.detail.customerGroupId;
      }
    })
  }
  //自然人编辑 关闭
  // function editPersonClose() {
  //   $('.z-Basic').css('display','none');
  //   $('#myModalWin').css('opacity','1');
  // }
  //自然人编辑身份信息
  function naturalPerEditInfor($event) {
    $('input[type=text]').click(function(ev){
      var ev=ev||event;
      if(ev.stopPropagation){
        ev.stopPropagation();
      }
      else{
        ev.cancelBubble=true;
      }

    });
    $('div.radio').click(function(ev){
      var ev=ev||event;
      if(ev.stopPropagation){
        ev.stopPropagation();
      }
      else{
        ev.cancelBubble=true;
      }

    })
    $('.details').click(function(ev){
      var ev=ev||event;
      if(ev.stopPropagation){
        ev.stopPropagation();
      }
      else{
        ev.cancelBubble=true;
      }
    })
    $('dd.fl select').click(function(ev){
      var ev=ev||event;
      if(ev.stopPropagation){
        ev.stopPropagation();
      }
      else{
        ev.cancelBubble=true;
      }

    })
    $('.L_naturalPerEditInforO').css('display','block');
    $('.L_naturalPerEditInforC').css('display','none');
    // $event.stopPropagation();
    $scope.editNaturalPersonInfoCopy = angular.copy($scope.editNaturalPersonInfo);


  }
  //自然人编辑身份信息取消
  function naturalPerEditInforCancel($event) {
    $('.L_naturalPerEditInforO').css('display','none');
    $('.L_naturalPerEditInforC').css('display','block');
    // $event.stopPropagation();
    $scope.editNaturalPersonInfo = $scope.editNaturalPersonInfoCopy;

  }


  //自然人编辑身份信息保存
  function saveEditPersonIdInfor(editNaturalPersonInfo) {
    for(var i=0;i<$scope.customerList.length;i++){
      if(editNaturalPersonInfo.customerGroupId == $scope.customerList[i].customerGroupId){
        $scope.groupName = $scope.customerList[i].customerGroupName;
      }
    }
    console.log(sessionStorage.getItem('customerGroupId'),'编辑保存');
    $scope.listActive=sessionStorage.getItem('customerGroupId');
    customerBarService.saveEditPersonIdInfor(editNaturalPersonInfo).then(function (response) {
      if(response.data.code == 1){
        $('.z-Basic').css('display','none');
        $('.modal-backdrop').css('display','none');
        $('.modal').css('display','none')
        $('.L_naturalPerEditInforO').css('display','none');
        $('.L_naturalPerEditInforC').css('display','block');
        // $scope.selectZiRanRenInfo();
        $scope.getPersonListGroup();
      }
    })
  }

  //自然人删除
  function deleteZRRCustomer(event,index,customerpersonId) {
    event.stopPropagation();
    var params = {'id':customerpersonId};
    bootbox.dialog({
      message: "确定删除自然人?",
      title: "删除自然人",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          className: "d-btn-confirm",
          callback: function () {
            customerBarService.deleteZRRCustomer(params).then(function (data) {
              if(data.code == 1){
                $scope.zrRestult.splice(index,1);
                // $scope.selectFaRenInfoList();
                $scope.getPersonListGroup();
              }
            })
          }
        },
        Cancel: {
          label: "取消",
          className: "d-btn-cancel",
          style:'background:red',
          callback: function () {

          }
        }

      }
    });
  }

  //编辑机构
  function editOrganiza(customercompanyId) {

    $('.z-Basic').css('display','block');
    $('.z-Basic-left').css('display','block');
    $('.z-Basic-header').css('display','block');
    $('.enterprise').css('display','block');
    $('.individual').css('display','none');
    $('.z-Basic-content').css('display','block');
    $('.details').css('display','block');
    $('.mechanism-on').css('display','block');
    $('.natural-on').css('display','none');
    $('.z-Basic-right').css('display','block');
    $('.sidebar-mechanism').css('display','block');
    $('.sidebar-natural').css('display','none');
    $('.L_organizaEditInforO').css('display','none');
    $('.L_organizaEditInforC').css('display','block');

    var params = {'id':customercompanyId };
    customerBarService.FRDetail(params).then(function (data) {
      if(data.code==1){
        $scope.editOrganizaInfor = data.data.detail;
        $scope.customerGroupId = $scope.editOrganizaInfor.customerGroupId;
        for(var i=0;i<$scope.customerList.length;i++){
          if($scope.customerGroupId == $scope.customerList[i].customerGroupId){
            $scope.groupName2 = $scope.customerList[i].customerGroupName;
          }
        }

        $scope.levels = data.data.levels;
        $scope.trades = data.data.trades;
      }
    })
  };

  //机构编辑身份信息
  function organizaEditInfor() {
    $('.L_organizaEditInforO').css('display','block');
    $('.L_organizaEditInforC').css('display','none');
    $scope.editOrganizaInforCopy = angular.copy($scope.editOrganizaInfor)

  }
  //机构编辑身份信息取消
  function organizaEditInforCancel() {
    $('.L_organizaEditInforO').css('display','none');
    $('.L_organizaEditInforC').css('display','block');
    $scope.editOrganizaInfor = $scope.editOrganizaInforCopy;
  }

  //机构编辑身份信息保存
  function saveEditOrganizaInfor(editOrganizaInfor) {
    for(var i=0;i<$scope.customerList.length;i++){
      if(editOrganizaInfor.customerGroupId == $scope.customerList[i].customerGroupId){
        $scope.groupName = $scope.customerList[i].customerGroupName;
      }
    }
    customerBarService.FREditorUrl(editOrganizaInfor).then(function (data) {
      if(data.code==1){
        $('.z-Basic').css('display','none');
         $('.modal-backdrop').css('display','none');
         $('.modal').css('display','none')
        $('.L_organizaEditInforO').css('display','none');
        $('.L_organizaEditInforC').css('display','block');
        // $scope.selectFaRenInfoList();
        $scope.selectFaRenInfo();
      }
    })
  };
  //机构删除
  function deleteFRCustomer(event,index,customercompanyId) {
    event.stopPropagation();
    var params = {'id':customercompanyId};
    bootbox.dialog({
      message: "确定删除机构?",
      title: "删除机构",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          className: "d-btn-confirm",
          callback: function () {
            customerBarService.deleteFRCustomer(params).then(function (data) {
              if(data.code==1){
                $scope.jgResult.splice(index,1);
                $scope.selectFaRenInfoList();
                // $scope.selectFaRenInfo();
              }
            })

          }
        },
        Cancel: {
          label: "取消",
          className: "d-btn-cancel",
          style:'background:red',
          callback: function () {

          }
        }

      }
    });


  };
  $scope.addParams={}
  //编辑分组
  function editGroupPort(item,type) {
    console.log(item,'编辑')
    if(type!=2){
      $scope.addParams.groupName=item.customerGroupName;
      $scope.addParams.groupId=item.customerGroupId;
    }else{
      $scope.addParams.groupName = ""
    }
    $scope.createTeam = true;
     // item.editGroup = true;
     // item.dropdownShow = false;
  }
  //下拉编辑分组
  function editGroupPortSec(item,tabShow){
    console.log(item,tabShow);
    $scope.editTeam=true;
    $scope.addParams.groupName=item.customerGroupName;
    $scope.addParams.groupId=item.customerGroupId;
    $scope.editTeamType=tabShow;
  }
  //保存编辑分组
  function saveEditGroup(item) {
    var params = {'groupId':$scope.addParams.groupId,'groupName':$scope.addParams.groupName};
    customerBarService.saveEditGroup(params).then(function (response) {
      if(response.data.code==1){
        // item.dropdownShow = false;
        // item.editGroup = false;
        if($scope.editTeamType==0){
          getPersonListGroup();
        }else{
          $scope.selectFaRenInfoList();
        }

        $scope.editTeam=false;
      }
    })
  }
  //删除分组
  function deleteGroup(index,item) {
    var params = {'groupId':item.customerGroupId,'status':2};
    bootbox.dialog({
      message: "确定删除此分组吗？",
      title: "删除此分组",
      className:'d-promptBox',
      buttons: {
        OK: {
          label: "确定",
          className: "d-btn-confirm",
          callback: function () {
            customerBarService.deleteEditGroup(params).then(function (response) {
              if(response.data.code==1){
                item.dropdownShow = false;
                $scope.customerList.splice(index,1);
                $scope.customerList2.splice(index,1)

              }
            })
          }
        },
        Cancel: {
          label: "取消",
          className: "d-btn-cancel",
          style:'background:red',
          callback: function () {

          }
        }

      }
    });

  };
  //编辑面板的导航
  $('.content-nav').children('li').click(function () {
    $(this).addClass('active');
    $(this).siblings('li').removeClass('active');
  });

  //移除box
  $scope.removeBox = function (event) {
    // event.stopPropagation();
    // $('#custmerUnfold').attr('aria-expanded','false');
    // $('.organizaDropdown').attr('aria-expanded',false);
  };
};
